package app

import (
	"bytes"
	"image/png"
)

const (
	four  = 4
	eight = 8
)

// Png2RGBA 格式转换.
func Png2RGBA(bs []byte) ([]byte, int32, int32) {
	img, _ := png.Decode(bytes.NewReader(bs))
	b := img.Bounds()
	w, h := b.Dx(), b.Dy()
	rgba := make([]byte, w*h*four)

	for j := 0; j < h; j++ {
		for i := 0; i < w; i++ {
			r, g, b, a := img.At(i, j).RGBA()
			rgba[four*(w*j+i)] = byte(r >> eight)
			rgba[four*(w*j+i)+1] = byte(g >> eight)
			rgba[four*(w*j+i)+2] = byte(b >> eight)
			rgba[four*(w*j+i)+3] = byte(a >> eight)
		}
	}

	return rgba, int32(w), int32(h)
}
